package com.quanyan.club.util;

import com.quanyan.club.entity.db.TbClubActivity;
import com.quanyan.club.entity.vo.resp.RespClubActivity;
import com.quanyan.club.task.tracker.ActivityCreateCycleRangeJob;
import com.quanyan.common.constants.Constants;
import com.quanyan.common.utils.CommonUtils;
import com.quanyan.common.utils.DateTimeUtils;
import com.quanyan.common.utils.DateUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;

import java.math.BigDecimal;
import java.util.*;

/**
 * Created by yue.yuan on 2016/12/2.
 */
public class ActivityUtils {

    private static Logger logger = Logger.getLogger(ActivityUtils.class);

    /**
     * 判断收费价格是否一致
     * @param tbClubActivity
     * @return
     */
    public static int isSamePrice(TbClubActivity tbClubActivity)
    {
        BigDecimal femaleMemberFee = tbClubActivity.getFemaleMemberFee();
        BigDecimal maleMemberFee = tbClubActivity.getMaleMemberFee();
        BigDecimal femaleFee = tbClubActivity.getFemaleFee();
        BigDecimal maleFee = tbClubActivity.getMaleFee();
        if(dequals(femaleMemberFee, maleMemberFee) &&
                dequals(maleMemberFee, femaleFee) &&
                dequals(femaleFee, maleFee) &&
                dequals(maleFee, femaleMemberFee))
        {
            return 1;
        }
        return 0;
    }

    /**
     * 判断两个价格大小
     * @param d1 小
     * @param d2 大
     * @return
     */
    public static boolean dequals(BigDecimal d1, BigDecimal d2) {
        if (null == d1 || null == d2) {
            return false;
        }
        if (Math.abs(d1.doubleValue() - d2.doubleValue()) < 0.00001) {
            return true;
        }
        return false;
    }

    /**
     * 判断是否活动时间是否包含黄金时段
     * @param startTime
     * @param endTime
     * @return
     */
    public static boolean isContainPrimeTime(Date startTime, Date endTime) {
        Calendar startdate = Calendar.getInstance(Locale.CHINA);
        startdate.setTime(startTime);
        int sh = startdate.get(Calendar.HOUR_OF_DAY);
        if (sh == 20 || sh == 19) {
            return true;
        }
        return false;
    }

    /**
     * 获取活动的最低收费
     * @param tbClubActivity
     * @return
     */
    public static BigDecimal getLowestPrice(TbClubActivity tbClubActivity)
    {
        if (null == tbClubActivity.getFeeType()) {//最低价格
            return BigDecimal.ZERO;
        }
        if (tbClubActivity.getFeeType().equals(Constants.BYTE_ZERO)) {
            return tbClubActivity.getAaFee();
        }
        List<Double> doubles = new ArrayList<>();
        doubles.add(tbClubActivity.getFemaleMemberFee().doubleValue());
        doubles.add(tbClubActivity.getMaleMemberFee().doubleValue());
        if(null == tbClubActivity.getSignUpType() ||
                tbClubActivity.getSignUpType().equals(Constants.BYTE_ZERO)) {
            doubles.add(tbClubActivity.getFemaleFee().doubleValue());
            doubles.add(tbClubActivity.getMaleFee().doubleValue());
        }
        return BigDecimal.valueOf(CommonUtils.mathUtil(doubles));
    }

    /**
     * 获取活动的最低收费
     * @param tbClubActivity
     * @return
     */
    public static BigDecimal getLowestPrice(RespClubActivity tbClubActivity)
    {
        if (null == tbClubActivity.getFeeType()) {//最低价格
            return BigDecimal.ZERO;
        }
        if (tbClubActivity.getFeeType().equals(Constants.BYTE_ZERO)) {
            return tbClubActivity.getAaFee();
        }
        List<Double> doubles = new ArrayList<>();
        doubles.add(tbClubActivity.getFemaleMemberFee().doubleValue());
        doubles.add(tbClubActivity.getMaleMemberFee().doubleValue());
        if(null == tbClubActivity.getSignUpType() ||
                tbClubActivity.getSignUpType().equals(Constants.BYTE_ZERO)) {
            doubles.add(tbClubActivity.getFemaleFee().doubleValue());
            doubles.add(tbClubActivity.getMaleFee().doubleValue());
        }
        return BigDecimal.valueOf(CommonUtils.mathUtil(doubles));
    }

    public static List<Integer> getDateParams(List<Integer> dateParams, RespClubActivity respClubActivity) {

        Calendar c = Calendar.getInstance();
        c.setTime(respClubActivity.getStartTime());
        Integer weekRange = null;
        if(c.get(Calendar.DAY_OF_WEEK) == 1){
            weekRange = 7;
        }else{
            weekRange = c.get(Calendar.DAY_OF_WEEK) - 1;
        }
        respClubActivity.setWeekRange(weekRange.byteValue());
        dateParams.add(respClubActivity.getWeekRange() + 2);
        if (isContainPrimeTime(respClubActivity.getStartTime(), respClubActivity.getEndTime())) {
            dateParams.add(2);//黄金时段
        }
        if (respClubActivity.getWeekRange() == 6 || respClubActivity.getWeekRange() == 7) {
            dateParams.add(1);//属于周末
        }
        return dateParams;
    }

    public static byte getStringType(String keyword)
    {
        keyword = keyword.toLowerCase();    //变为小写
        //1 按汉字搜 2 按英文搜 3 按首字母搜 4汉英混合搜
        boolean containsEN = ClubNameUtils.clubNameContainsEN(keyword);
        boolean containsCN = ClubNameUtils.clubNameContainsCN(keyword);
        if (containsCN && !containsEN){     //只包含汉字
            return (byte)1;
        }
        if (containsEN && !containsCN){     //只包含英文
            return (byte)2;
        }
        if (containsEN && !containsCN && keyword.length() == 1
                && !StringUtils.isNumeric(keyword)){     //只包含英文一个字母
            return (byte)3;
        }
        if (containsCN && containsEN){      //包含中英文
            return (byte)4;
        }
        return 4;
    }


    public static String sortString(String cycleRange)
    {
        StringBuffer sb = new StringBuffer();
        String[] cycles = cycleRange.split(",");
        for(int i = 0;i < cycles.length; i++)
        {
            for(int j = i+1;j < cycles.length;j++)
            {
                int aa = Integer.parseInt(cycles[i]);
                int bb = Integer.parseInt(cycles[j]);
                if(aa >= bb)
                {
                    int cc = aa;
                    cycles[i] = String.valueOf(bb);
                    cycles[j] = String.valueOf(aa);
                }
            }
            sb.append(cycles[i]);
            if(i < cycles.length - 1)
            {
                sb.append(",");
            }
        }
        return sb.toString();
    }

}
